<?php
$target_dir = "uploads/";
//$datum = mktime(date('H')+0, date('i'), date('s'), date('m'), date('d'), date('y'));
//$target_file = $target_dir . date('Y.m.d_H:i:s_', $datum) . $_FILES["file"]["name"];
$target_file = $target_dir . $_FILES["file"]["name"];
$uploadOk = 1;
//print_r($_FILES);;
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);   // 获取文件后缀名

// 判断是否是允许的图片格式
if (!in_array($extension, $allowedExts)) {
     echo "<font color=\"red\">文件格式错误，仅支持JPG, JPEG, PNG & GIF等图片格式文件！</font>";
     $uploadOk = 0;
}
// 判断是否是合法图片
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["file"]["tmp_name"]);
  if($check !== false) {
    //echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  }
  else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}
// 检验图片尺寸
if ($_FILES["file"]["size"] > 1048576) {
   echo "Sorry, your file is too large.";
   $uploadOk = 0;
}
// 是否已存在同名图片
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  //  echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
  //  echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
  //  echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  //  echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>";
  //  echo "文件存储在: " . $target_file . "<br>";
  //  echo "<br>";
      move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
      print("<监控 by esp32cam：>");
      print("http://" . get_onlineip() . "/" . $target_file); 
}

function get_onlineip() {
  return "101.43.241.226";
}

?>
